package pessoas 
{
	import flash.media.Microphone;
	import org.flixel.FlxPoint;
	import org.flixel.FlxSprite;
	import org.flixel.FlxText;
	import org.flixel.FlxState;
	/**
	 * ...
	 * @author João Brant
	 */
	public class PessoaSugavel extends Pessoa
	{
		private var suckedCount:int = 0;
		private var beingSucked:Boolean = false;
		public var money:int;
		private var velocityBefore:FlxPoint = new FlxPoint();
		
		private var moneyText:FlxText = new FlxText( -100, -100, 100, "$");
		
		private var direction:int;
		
		public var floatingMoney:FlxSprite;
		public var floatingMoneyTimer:Number = -1;
		public var floatingMoneyInterval:Number = 0.1;
		
		public function PessoaSugavel() 
		{
			money = 0;
			moneyText.color = 0xff88ff88;
		}
		
		override public function init(state:MainState, pos:FlxPoint, dir:int, _head:Class, _body:Class, _legs:Class, _arms:Class):void
		{
			direction = dir;
			
			super.init(state, pos, dir, _head, _body, _legs, _arms);
		}
		
		override public function update():void 
		{
			super.update();
			
			floatingMoneyTimer -= Main.delta;
			
			if (beingSucked)
			{
				suckedCount++;
				this.setScale(direction + Math.sin(suckedCount) * 0.2, 1 + Math.cos(suckedCount) * 0.2);
				
				if (floatingMoneyTimer < 0 && money > 0)
				{
					//Para evitar de ficar saindo dinheiro mesmo muito infame
					var rPop:Number = Math.random();
					var appearChance:Number = (state.popularity * 0.01) * (state.popularity * 0.01); //Ao quadrado pra ficar mais dramatico perder pop
					if (rPop < appearChance)
					{
						floatingMoney = randomMoney();
						floatingMoneyTimer = floatingMoneyInterval;
						state.moneyGroup.add(floatingMoney);
					}
				}
			}
			else
			{
				this.setScale(direction, 1);
			}
			
			moneyText.x = this.x + 8;
			moneyText.y = this.y - 28;
			moneyText.text = money + "$";
			moneyText.alpha = _alpha;
			
			if (this.money <= 0) die();
		}
		
		override public function draw():void
		{
			super.draw();
			//if (!dead)
				moneyText.draw();
		}
		
		public function beginSuck():void
		{
			if (beingSucked)
				return;
			
			velocityBefore.x = velocity.x;
			velocityBefore.y = velocity.y;
			
			beingSucked = true;
			
			this.velocity = new FlxPoint();
			
			if (this.money <= 0)
			{
				die();
			}
		}
		
		public function endSuck():void
		{
			if (!beingSucked)
				return;
			
			beingSucked = false;
			
			velocity.x = velocityBefore.x;
			velocity.y = velocityBefore.y;
			
			/*if (this.money <= 0)
			{
				die();
			}*/
		}
		
		public function randomMoney():Money
		{
			var r:Number = Math.random();
		
			if (this is Rico)
			{
				if (r < .6)
				{
					return new Money(x + 16, y + 16, 0, state.salazar);
				}
				else if (r < .7)
				{
					return new Money(x + 16, y + 16, 1, state.salazar);
				}
				else if (r < .8)
				{
					return new Money(x + 16, y + 16, 2, state.salazar);
				}
				else if (r < .9)
				{
					return new Money(x + 16, y + 16, 3, state.salazar);
				}
				else
				{
					return new Money(x + 16, y + 16, 4, state.salazar);
				}
			}
			else
			{
				if (r < .8)
				{
					return new Money(x + 16, y + 16, 0, state.salazar);
				}
				else
				{
					return new Money(x + 16, y + 16, 1, state.salazar);
				}
			}
			
			return null;
		}
	}

}